package com.edgclub.edg.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.RechargeApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 支付管理
 */
public class  GzPayPassForgetActivity extends GzBaseTooolBarActivity implements
        OnClickListener, VolleyListener {

    private Activity activity;
    private Button btn_login, next_btn;



    private boolean isLoading = false; // 是否正在加载
    private UserPrefUtils userPrefUtils;// 用户信息存储
    private RechargeApi rechargeApi;
    private boolean isFirst = true;
    private String modifyCode="";
    private String frompage="";
    private EditText mess_code;
    // 计时器
    private int time = 120;
    private Timer timer;
    private TimerTask timerTask;
    private String CodeNum="";


    @Override
    public int getContentView() {
        activity = this;
        return R.layout.activity_gz_apply_pass_forget;

    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        frompage=getIntent().getStringExtra("frompage");
        if ("forget".equals(frompage)) {
            title.setText("忘记支付密码");
        } else {
            title.setText("设置支付密码");
        }
    }

    @Override
    public void initView() {
        btn_login = (Button) findViewById(R.id.btn_login);
        next_btn = (Button) findViewById(R.id.next_btn);
        mess_code = (EditText) findViewById(R.id.mess_code);
        btn_login.setOnClickListener(this);
        next_btn.setOnClickListener(this);

    }
    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
        rechargeApi = new RechargeApi(this);

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_login:
                getCode();
                break;
            case R.id.next_btn:
                if(TextUtils.isEmpty(mess_code.getText().toString())){
                    showToast("请输入验证码");
                    return;
                }
                oksubmit();
//              finish();
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode==1){
                finish();
            }
        }
    }


    private void getCode() {
        UserParam param = new UserParam();
        param.setUid(userPrefUtils.getUser().getId() + "");
        param.setTarget("setPayPwd");
        new UserApi(this).registerGetcode(param,this);




//        if (isLoading) {
//            return;
//        }
//        UserParam atMeParam = new UserParam();
//        atMeParam.setUserId(userPrefUtils.getUser().getId() + "");
//        if ("setpass".equals(frompage)) {
//            rechargeApi.getCode(atMeParam,this);
//        }else if("forget".equals(frompage)){
//            rechargeApi.getCode1(atMeParam, this);
//        }
    }

    public void oksubmit() {

        UserParam param = new UserParam();
        param.setUid(userPrefUtils.getUser().getId() + "");
        param.setTarget("setPayPwd");
        param.setCode(mess_code.getText().toString().trim());
        new UserApi(this).registerCode(param,this);
    }

    @Override
    public void onStart(String tag) {
        if (ApiConfig.APIURL.URL_REGISTER_CODE .equals(tag)) {
            showDialog(StringUtils.getString(R.string.get_identifying), false);
        }else if (ApiConfig.APIURL.URL_REGISTER_CODE1 .equals(tag)){
            showDialog(StringUtils.getString(R.string.get_login), false);
        }
    }

    @Override
    public void onSuccess(String response, String tag) {
        isLoading = false;
        dismissDialog();
        if (ApiConfig.APIURL.URL_REGISTER_CODE .equals(tag)) {
            if (JSONHelper.isSuccess(response)) {
                JSONObject jsonObject;

                try {
                    jsonObject = new JSONObject(response);
                    CodeNum = jsonObject.optString("data") + "";
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Toast.makeText(GzPayPassForgetActivity.this, StringUtils.getString(R.string.checkverification),
                        Toast.LENGTH_LONG).show();
                btn_login.setEnabled(false);
                startTimer();
            }
            if (JSONHelper.getResultCode(response) == 1003) {
                Toast.makeText(GzPayPassForgetActivity.this,
                        StringUtils.getString(R.string.verificationerror), Toast.LENGTH_LONG).show();

            }

        }else if (ApiConfig.APIURL.URL_REGISTER_CODE1 .equals(tag)) {
            if (JSONHelper.isSuccess(response)) {
                modifyCode = mess_code.getText().toString().trim();
                Intent i2 = new Intent(GzPayPassForgetActivity.this, GzPayPassUpdateActivity.class);
                i2.putExtra("modifyCode",modifyCode);
                if("setpass".equals(frompage)){
                    i2.putExtra("frompage","setpass");
                }else if("forget".equals(frompage)){
                    i2.putExtra("frompage","forget");
                }
                startActivityForResult(i2,1);

            } else if (JSONHelper.getResultCode(response) == 1003) {
                Toast.makeText(GzPayPassForgetActivity.this,
                        StringUtils.getString(R.string.verificationerror), Toast.LENGTH_LONG).show();

            }
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        isLoading = false;
        dismissDialog();
        ResultCode.toastVolleyError(activity, volleyError);
        Toast.makeText(
                GzPayPassForgetActivity.this,
                StringUtils.getString(R.string.verificationfail) , Toast.LENGTH_LONG).show();
    }



    class CountDownTimerTask extends TimerTask {

        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    }
    private boolean isrun;
    private void startTimer() {
        isrun=false;
        btn_login.setBackgroundResource(R.drawable.register_selector);
        btn_login.setTextColor(getResources().getColor(R.color.c5));
//        btn_login.setBackgroundResource(R.drawable.shape_gz_get_code_bg2);
        time = 60;
        if (null == timer) {
            timer = new Timer();
        }

        timerTask = new CountDownTimerTask();
        timer.schedule(timerTask, 1000, 1000);
    }

    private void stopTimer() {
        isrun=true;
        btn_login.setBackgroundResource(R.drawable.shape_gz_get_code_bg);
        btn_login.setTextColor(getResources().getColor(R.color.c10));
//        btn_login.setBackgroundResource(R.drawable.shape_gz_get_code_bg);
        if (null != timer) {
            timer.cancel();
            timer = null;
        }

        if (null != timerTask) {
            timerTask.cancel();
        }

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    time--;
                    btn_login.setText(time + StringUtils.getString(R.string.sresend));
                    if (time == 0) {
                        stopTimer();
                        time = 60;
                        btn_login.setEnabled(true);
                        btn_login.setText(StringUtils.getString(R.string.sendverification));
                    }
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };
}
